<template>
	<view class="my-path-list">
		<view class="path-list">
			<view
				v-for="(item, index) in list"
				:key="index"
				
			>
				<view class="path-item">
					 
					<view @tap="goConfirmOrder(item)">
						<view class="item-main">
							<view class="item-name">{{ item.contact_name }}</view>
							<view>{{ item.contact_phone }}</view>
						</view>
						<view class="item-main">
							<view>{{ item.province }} {{ item.city }} {{ item.district }} {{ item.address }}</view>
						</view>
						<!-- <view style="margin: 20rpx 0">
							<u-line></u-line>
						</view> -->
					</view>
					<view class="set-default">
						<!-- <view class="radio" @tap="radioChange(item)">
							<radio-group >
								<radio
									style="transform: scale(0.7)"
									:checked="item.isDefault"
									color="#1786f8"
								></radio>
							</radio-group>
						</view> -->
						<view></view>
						<view style="">
							<text @tap="toAddPath('update',item)" style="display: inline-block;padding: 10rpx 20rpx;background: #1786f8;color: #fff;border-radius: 30rpx;font-size: 24rpx;">编辑</text>
							<text @tap="toDelPath(item)" style="display: inline-block;padding: 10rpx 20rpx;background: #f82427;color: #fff;border-radius: 30rpx;font-size: 24rpx;margin-left: 20rpx;">删除</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="add-path">
			<view class="add-path-btn" @tap="getAddPath">一键获取</view>
			<view class="add-path-btn" @tap="toAddPath('add')">新增地址</view>
		</view>
	</view>
</template>

<script>
import { addressList , addaddress , upaddress , deladdress } from '../../api/address'; 
export default {
	data() {
		return {
			isDefault: '',
			lastPage:'',
			list: [
				// {
				// 	name: 1,
				// 	tel: '1231321321',
				// 	isDefault: true,
				// 	address: 'asdgasgasdgasdgsdgsdg'
				// },
				// {
				// 	name: 1,
				// 	tel: '1231321321',
				// 	isDefault: false,
				// 	address: 'asdgasgasdgasdgsdgsdg'
				// }
			]
		};
	},
	onShow(){
		this.getAddressList()
	},
	onLoad(options) {
		this.lastPage = options.lastPage
	},
	methods: {
		getAddressList(){
			addressList().then(res => {
				this.list = res.data
			})
		},
		goConfirmOrder(val) {
			if(this.lastPage == 'order'){
				uni.navigateBack({
				  delta: 1,  // 返回上一级页面
				  success: function () {
				    // 向上一级页面传递参数 
				    uni.$emit('onBack', val);
				  }
				});
			}
		},
		getAddPath() {
		  // 保存当前对象的引用，确保在异步回调中能够访问
		  const that = this;
		
		  // 使用 Promise 包装 uni.chooseAddress，以便更好地处理异步操作
		  return new Promise((resolve, reject) => {
		    uni.chooseAddress({
		      success(res) {
		        // 调用 addaddress 函数添加地址信息
		        addaddress({
		          province: res.provinceName,
		          city: res.cityName,
		          district: res.countyName,
		          address: res.detailInfo,
		          zip: res.nationalCode,
		          contact_name: res.userName,
		          contact_phone: res.telNumber
		        }).then(response => {
		          // 处理成功的情况
		          if (response.status == 200) {
		            // 更新地址列表
		            that.getAddressList();
		          }
		          // 显示操作结果信息
		          uni.showToast({
		            title: response.message
		          });
		          // 成功执行 Promise
		          resolve(response);
		        }).catch(error => {
		          // 处理 addaddress 函数调用失败的情况
		          uni.showToast({
		            title: '添加地址失败，请稍后重试'
		          });
		          // 失败执行 Promise
		          reject(error);
		        });
		      },
		      fail(error) {
		        // 处理用户选择地址操作失败的情况
		        uni.showToast({
		          title: '选择地址失败，请稍后重试'
		        });
		        // 失败执行 Promise
		        reject(error);
		      }
		    });
		  });
		},
		toAddPath(type,val) {
			uni.navigateTo({
				url: './addrPath?type=' +  type + '&val=' + JSON.stringify(val)
			});
		},
		// 删除收货地址
		toDelPath(val){
			uni.showModal({
				title: '操作提示',
				content: "确定要删除该地址吗?",
				success: (res) => {
					if (res.confirm) {
						console.log(val)
						deladdress(val.id).then(r => {
							if(r.status == 200){
								uni.showToast({
									title:'删除成功'
								})
								this.getAddressList()
							}
						})
					}
				},
			})	
		},
		radioChange(item) {
			this.list.forEach((i) => {
				this.$set(i, 'isDefault', false);
				// i.isDefault= false;
			});
			// item.isDefault=true
			this.$set(item, 'isDefault', true);
			// this.$forceUpdate();
			// console.log(this.list);
		}
	}
};
</script>

<style scoped>
.set-default {
	display: flex;
	justify-content: space-between;
	height: 30rpx;
	align-items: center;
	flex: 1;
	margin-top: 15rpx;
}
.radio {
	display: flex;
}
.path-list {
	padding: 30rpx;
}
.path-item {
	border-radius: 10rpx;
	padding: 30rpx;
	background-color: #ffffff;
	margin-bottom: 30rpx;
	font-size: 26rpx;
}
.item-main {
	display: flex;
	align-items: center;
}
.item-name {
	padding-right: 10rpx;
}

.add-path {
	padding: 20rpx;
	width: 100%;
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
}
.add-path-btn {
	border: 2rpx solid #1786f8;
	color: #1786f8;
	border-radius: 50rpx;
	padding: 10rpx 0;
	margin-bottom: 20rpx;
	width: 50%;
	text-align: center;
	
}
.add-path-btn:first-child{
	background-color: #1786f8;
	color: #ffffff;
}
</style>
